package com.leyou.aop;

import com.leyou.annotation.AuthorityForRCheck;
import com.leyou.common.pojo.R;
import com.leyou.common.pojo.Result;
import com.leyou.annotation.AuthorityForResultCheck;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Set;


/**
 * @author xhh
 * @describe 描述
 * @date 2022/7/27 8:26
 * @ClassName MyAopAnnotation
 */

@Slf4j
@Component
@Aspect
public class AuthorityAnnotation {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Pointcut("@annotation(com.leyou.annotation.AuthorityForRCheck)")
    public void annotationWhere(){
    }
    @Pointcut("@annotation(com.leyou.annotation.AuthorityForResultCheck)")
    public void annotationForResultWhere(){
    }


    @Around("annotationWhere()")
    public Object authorityAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("进入aop环绕");
        if(request != null){
            // Do something
            String managerAccountNumber = (String)request.getAttribute("managerAccountNumber");
            if(managerAccountNumber!=null){
                //根据已经登录的管理员编号 获取权限列表
                Set members = redisTemplate.opsForSet().members(managerAccountNumber);
                //获取注解的所需要的权限
                MethodSignature signature = (MethodSignature)joinPoint.getSignature();
                Method method = signature.getMethod();
                AuthorityForRCheck annotation = method.getAnnotation(AuthorityForRCheck.class);
                //比较是否存在
                for (Object member : members) {
                    String authorityNumber=member.toString();
                    if(authorityNumber.equals("main")){
                        return joinPoint.proceed();
                    }
                    if(annotation.authority().equals(authorityNumber)){
                        return joinPoint.proceed();
                    }
                }
                return R.error();
            }else{
                return R.error();
            }
        }else{
            return R.error();
        }
    }
    @Around("annotationForResultWhere()")
    public Object authorityForResultAround(ProceedingJoinPoint joinPoint) throws Throwable {

        log.info("进入aopResult环绕");
        //RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        log.info("1");
        if(request!= null){
            //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION);
            //HttpSession session = request.getSession();
            // Do something
            String managerAccountNumber = (String)request.getAttribute("managerAccountNumber");
            if(managerAccountNumber!=null){
                //根据已经登录的管理员编号 获取权限列表
                Set members = redisTemplate.opsForSet().members(managerAccountNumber);
                //获取注解的所需要的权限
                MethodSignature signature = (MethodSignature)joinPoint.getSignature();
                Method method = signature.getMethod();
                AuthorityForResultCheck annotation = method.getAnnotation(AuthorityForResultCheck.class);
                System.out.println(annotation);
                log.info("{},{}",members,annotation.authority());
                //比较是否存在
                for (Object member : members) {
                    String authorityNumber=member.toString();
                    if(authorityNumber.equals("main")){
                        return joinPoint.proceed();
                    }
                    if(annotation.authority().equals(authorityNumber)){
                        return joinPoint.proceed();
                    }
                }
                return new Result();
            }else{
                return new Result();
            }
        }else{
            return new Result();
        }
    }
}
